//
//  JSEditAddressController.m
//  jshop
//
//  Created by 张艳江 on 2019/5/18.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCEditAddressController.h"
#import "LSCityChooseView.h"

@interface SCEditAddressController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UILabel     *addressLab;
@property (weak, nonatomic) IBOutlet UITextField *detailAddressTF;
@property (weak, nonatomic) IBOutlet UISwitch    *defSwitch;

@property (strong, nonatomic) NSArray  *addressArray;
@property (strong, nonatomic) NSString *area_id;
@property (strong, nonatomic) NSString *is_def;

@end

@implementation SCEditAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self.title isEqualToString:@"添加地址"]) {
        self.is_def = @"0";
        self.defSwitch.on = NO;
    }else{
        self.is_def = self.model.is_def;
        if ([self.is_def intValue] == 1) {
            self.defSwitch.on = YES;
        }else{
            self.defSwitch.on = NO;
        }
        self.nameTF.text = self.model.name;
        self.phoneTF.text = self.model.phone;
        self.addressLab.text = self.model.adress;
        self.addressLab.textColor = [UIColor blackColor];
        self.detailAddressTF.text = self.model.detail;
        self.area_id = self.model.adress_id;
    }
}

- (IBAction)clickAddressBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    
    LSCityChooseView *view = [[LSCityChooseView alloc] init];
    WeakSelf;
    view.selectedBlock = ^(NSString *province, NSString *city, NSString *area){
        weakSelf.addressLab.text = [NSString stringWithFormat:@"%@%@%@",province,city,area];
        weakSelf.addressLab.textColor = [UIColor blackColor];
    };
    [view showPickerView];
}
- (IBAction)clickSwitchBtn:(UISwitch *)sender {
    
    [self.view endEditing:YES];
    sender.on = !sender.on;
    if (sender.on == YES) {
        self.is_def = @"1";
    }else{
        self.is_def = @"0";
    }
}
- (IBAction)clickSubmitBtn:(id)sender {
    
    if (self.nameTF.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"请输入收货人姓名"];
        return;
    }
    if (self.phoneTF.text.length != 11) {
        [SVProgressHUD showImage:nil status:@"请输入正确的手机号码"];
        return;
    }
    if ([self.addressLab.text isEqualToString:@"请选择收货地址"]) {
        [SVProgressHUD showImage:nil status:@"请选择收货地址"];
        return;
    }
    if (self.detailAddressTF.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"请输入详细的收货地址"];
        return;
    }
    NSDictionary *dict;
    NSString *url;
    if ([self.title isEqualToString:@"添加地址"]) {
        dict = @{@"name":self.nameTF.text,
                 @"adress":self.addressLab.text,
                 @"phone":self.phoneTF.text,
                 @"default":self.is_def,
                 @"detail":self.detailAddressTF.text,
                 @"token":Token};
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_address_add"];
    }else{
        dict = @{@"name":self.nameTF.text,
                 @"adress":self.addressLab.text,
                 @"phone":self.phoneTF.text,
                 @"default":self.is_def,
                 @"detail":self.detailAddressTF.text,
                 @"token":Token,
                 @"adress_id":self.model.adress_id};
        NSLog(@"提交的数据%@",dict);
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_address_edit_do"];
    }
    [SVProgressHUD showWithStatus:@"提交中..."];
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
        if (self.refreshData) {
            self.refreshData();
        }
        [self.navigationController popViewControllerAnimated:YES];
    }];
}



@end
